package com.ikonke.iot.alarm.es;

import java.io.IOException;

import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class EsClient {

    private static volatile RestHighLevelClient esClient;

    private EsClient() {

    }

    public static RestHighLevelClient getEsClient() {
        if (esClient == null) {
            synchronized (EsClient.class) {
                if (esClient == null) {
                    // esClient = new RestHighLevelClient(RestClient.builder(new HttpHost("172.24.100.118", 9200, "http")));
                    esClient = new RestHighLevelClient(RestClient.builder(new HttpHost("172.25.240.37", 9200, "http")));
                }
            }
        }
        return esClient;
    }

    public static void close() {
        try {
            if (esClient != null) {
                synchronized (EsClient.class) {
                    if (esClient != null) {
                        esClient.close();
                        esClient = null;
                    }
                }
            }

        } catch (IOException e) {
            log.error("close rest high level client error.", e);
        }

    }
}
